﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web; 
using log4net;

namespace WebApplication3.Core
{

    public static class Log
    {
        /// <summary>
        /// 一般错误
        /// 
        /// </summary>
        /// <param name="message">消息</param>
        public static void Error(object message)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Error(message);
        }

        /// <summary>
        /// 一般错误
        /// 
        /// </summary>
        /// <param name="message">消息</param><param name="exception">异常</param>
        public static void Error(object message, Exception exception)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Error(message, exception);
        }

        /// <summary>
        /// 信息
        /// 
        /// </summary>
        /// <param name="message">消息</param>
        public static void Info(object message)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Info(message);
        }

        /// <summary>
        /// 信息
        /// 
        /// </summary>
        /// <param name="message">消息</param><param name="exception">异常</param>
        public static void Info(object message, Exception ex)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Info(message, ex);
        }

        /// <summary>
        /// 警告
        /// 
        /// </summary>
        /// <param name="message">消息</param>
        public static void Warn(object message)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Warn(message);
        }

        /// <summary>
        /// 警告
        /// 
        /// </summary>
        /// <param name="message">消息</param><param name="exception">异常</param>
        public static void Warn(object message, Exception ex)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Warn(message, ex);
        }

        /// <summary>
        /// 调试
        /// 
        /// </summary>
        /// <param name="message">消息</param>
        public static void Debug(object message)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Debug(message);
        }

        /// <summary>
        /// 调试
        /// 
        /// </summary>
        /// <param name="message">消息</param><param name="exception">异常</param>
        public static void Debug(object message, Exception ex)
        {
            LogManager.GetLogger(Log.GetCurrentMethodFullName()).Debug(message, ex);
        }

        private static string GetCurrentMethodFullName()
        {
            try
            {
                int num = 2;
                StackTrace stackTrace = new StackTrace();
                int length = stackTrace.GetFrames().Length;
                StackFrame frame;
                string str;
                do
                {
                    frame = stackTrace.GetFrame(num++);
                    str = frame.GetMethod().DeclaringType.ToString();
                }
                while (str.EndsWith("Exception") && num < length);
                string name = frame.GetMethod().Name;
                return str + "." + name;
            }
            catch
            {
                return (string)null;
            }
        }
    }
}



 